package com.hbx.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/*
 * @Author:hbx
 * 
 * */
@Component
@Slf4j
public class CacheUtils {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;


    /**
     * key-value形式写入，生存时间expire，单位s
     * @param key
     * @param value
     * @param expire
     */
    public void set(String key,String value,Long expire){
        try {
            redisTemplate.opsForValue().set(key, value, expire, TimeUnit.SECONDS);
            log.info("【缓存模块】:key={}写入缓存成功,有效期{}s",key,expire);
        } catch (Exception e) {
            log.error("【缓存模块】:key={},value={},expire={}写入缓存失败",key,value,expire);
        }
    }

    /**
     * 查询key
     * @param key
     * @return
     */
    public String get(String key){
        String result = null;
        try {
            result = (String) redisTemplate.opsForValue().get(key);
            log.info("【缓存模块】:查询到key={}的结果,{}",key,result);
        } catch (Exception e) {
            log.error("【缓存模块】:查询到key={}的结果失败",key);
        }
        return result;
    }

    /**
     * key自增
     * @param key
     */
    public void incr(String key){
        try {
            Long result = redisTemplate.opsForValue().increment(key);
            log.info("【缓存模块】:key:{}自增的结果,{}",key,result);
        } catch (Exception e) {
            log.error("【缓存模块】:key:{}自增失败",key);
        }
    }

    /**
     * 将values存到set中
     * @param key
     * @param values
     */
    public Long add(String key, Object[] values){
        Long result= 0L;
        try {
            result = redisTemplate.opsForSet().add(key, values);
            log.info("【缓存模块】:key:{}存入成功",key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【缓存模块】:key:{},values:{}存入失败", key, values);
        }
        return result;
    }

    /**
     * 获取key值
     * @param key
     * @return
     */
    public Set<Object> getSetMembers(String key){
        Set<Object> result = null;
        try {
            result = redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【缓存模块】:key{}获取失败", key);
        }
        return result;
    }

    /**
     * 移除元素
     * @param key
     * @param values
     * @return
     */
    public Long setRemove (String key, Object[] values){
        Long result= 0L;
        try {
            result = redisTemplate.opsForSet().remove(key, values);
            log.info("【缓存模块】:操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【缓存模块】:key:{},values:{}操作失败", key, values);
        }
        return result;
    }

    /**
     * keys模糊查询
     * @param key
     * @return
     */
    public Set<String> keys(String key){
        Set<String> result = null;
        try {
            result = redisTemplate.keys(key + "*");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("【缓存模块】:key:{}模糊匹配查询失败", key);
        }
        return result;
    }

    /**
     * 删除key
     * @param key
     * @return
     */
    public Boolean removeKey(String key) {
        try {
            return redisTemplate.delete(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 删除key
     * @param keys
     * @return
     */
    public Long removeKey(Collection<String> keys) {
        try {
            return redisTemplate.delete(keys);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }
}
